package ui;

import javax.swing.*;
import javax.swing.border.*;

import ui.handlers.OrderHandler;
import java.awt.*;


import java.rmi.RemoteException;
import java.util.ArrayList;

public class ChefHomeScreen extends BaseScreen {

	private JFrame mainFrame;
	JPanel mainContent_panel, menu_panel;
	JButton complete_button;
	
	/* Methods needed by Handler */
	
	public JButton getCompleteButton()
	{ return complete_button; }
	
	public JPanel getMainContent()
	{ return mainContent_panel; }
	
	public JPanel getMenuContent()
	{ return menu_panel; }
	
	public JFrame getMainFrame()
	{ return mainFrame; }
	
	/* END OF HANDLER METHODS */
	
	public ChefHomeScreen()
	{
		mainFrame = new JFrame();
		mainFrame.setTitle("Chef: View Orders");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		mainContent_panel = new JPanel();
		
		mainFrame.add(mainContent_panel);
		doTitle();
		try {
			doOrder();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		doButton();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String text = "<HTML><FONT SIZE = \"6\">"+
				"Order</FONT></HTML>";
		JLabel title_lbl = new JLabel(text);
		title_lbl.setBorder(new EmptyBorder(15, 20, 20, 0));
		
		mainFrame.add(title_lbl, BorderLayout.NORTH);
	}
	
	public void doOrder() throws RemoteException
	{
		menu_panel = new JPanel();
		
		if (store.submittedOrdersEmpty())
		{ menu_panel.add(new JLabel("All orders complete")); }
		else
		{
			int orderId = store.getFirstSubmittedOrderId();
			ArrayList<Integer> orderItems = store.getFirstSubmittedOrderItems();
			
			menu_panel.setLayout(new GridLayout(orderItems.size(), 1));
			
			for (int itemId: orderItems)
			{
				JPanel item_panel = new JPanel();
				item_panel.setLayout(new FlowLayout());
				
				int quantity = store.getOrderItemQuantity(orderId, itemId);
				String name = store.getMenuItemName(itemId);
				
				item_panel.add(new JLabel(name));
				item_panel.add(new JLabel("x"+quantity));
				
				menu_panel.add(item_panel);
			}
		}
		
		menu_panel.validate();
		mainContent_panel.add(menu_panel);
	}
	
	private void doButton()
	{
		complete_button = new JButton("Complete");
		Font btnFont = new Font(complete_button.getFont().getName(),complete_button.getFont().getStyle(), 16);
		complete_button.setFont(btnFont);
		complete_button.addActionListener(new OrderHandler(this));
		
		JPanel button_panel = new JPanel();
		button_panel.setBorder(new EmptyBorder(0, 0, 100, 0));
		button_panel.add(complete_button);
		
		mainFrame.add(button_panel, BorderLayout.SOUTH);
	}
}